<?php
/**
 * 测试样例类
 *
 * PHP VERSION = PHP 7.2.20
 */
namespace joinpay;
require "Autoload.php";

use joinpay\Request;
use joinpay\SecretKey;
use joinpay\RequestUtil;
use joinpay\RandomUtil;
use joinpay\AESUtil;

$requestdata=$_REQUEST;



//平台公钥
$platPublicKey = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCl3h19MlofijMPj3RGYVyuwBxCifjSuqO5fPCpp+L3wkE8xyXAJ1Q18/EkKRzorcXiQmlnMCpyToJqygMHdokKQWYns4rImdXDRYyPFaqb4wl8XvANx/a3y6A9QXtJ2QTbzOGFX1k0vJ2M9JAzAb+2/PMomiQF1Zv4q40L23o+2wIDAQAB
-----END PUBLIC KEY-----";

//商户私钥
$mchPrivateKey = "-----BEGIN RSA PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANVdQUwKFFj5+E+TjPe+uqA/5tzBeBS5IxrDWvQ/v4KWAZ/Sby7+FPb7Wk8gCOhkTmLhJYwBuT2BOR93/wQMWkmul8Rs6mw6fej/ii38u8m8EzG6n+bBBELjfuSqFfSbM1cnTpU4dvd6LNVn8kMK6yXoHIzqINBZ6CckE9wjsCl5AgMBAAECgYAgmT/puzyCn6aFmfiWvYhMBZdjMRgEqoinhSN3X+oFrJ+Q6JjeKCnR1nNk1XMm9nRXfSi8bGL996lfmnjXCIAerEVo+sbgCdhgSGvY0UiLEzpw4zT3hq0W6WnAMuI+qdjHU3fAZLO+NdWTlDYiBydhWfAo/0gQw2rzSU6f9LSbAQJBAPizmCOH3f3JALqM2xe873NV3RaTuH9o1AOFrRlFMZKYkdXjp0y9Y0WnZRxevc8NmllV95O6vI3n7Ne6JY0Sn9kCQQDboC8/ZGAlvLOvEA7iMxOPPVCcOlXk1dAECAErQMF/TLyRYj1/oAo7bgJJb55b0DzNz25cyfChVlgaPepaQ3KhAkEAk9HjRpMV/ib/eOIJwyU4SZwqaaXpjkkXYvSMOy3mVhS68Z97K+CRZbAmJ+Oj+7JBIII5QiRGuC22l057zUrZmQJAFOlLll2YSf+NnD2LLT5vvlO4OSfUxdUV2Uxp0BR6WASWb8FK2bwDPWVcEmHuUg+hunblqzAPGU1/VgBPMAj8AQJBAKQZXqaxtguRUq5k7g1q0dpzfEjxuHXJ/RHWZwbs3usBkUF7ThZ2JrEj1PWJLrQsDQtcdkaogY+6P6Y1MS56JYs=
-----END RSA PRIVATE KEY-----";

$secKey = RandomUtil::randomStr(16);



$data = [];
$data2 = [];
$data["mch_no_trade"] = "";
$data2["mch_no_trade"] = $data["mch_no_trade"];

$data["mch_order_no"] = !empty($requestdata['mch_order_nos'])?$requestdata['mch_order_nos']:"";
$data2["mch_order_no"] = !empty($requestdata['mch_order_no'])?$requestdata['mch_order_no']:"";

 
$data["order_amount"] = !empty($requestdata['order_amount'])?$requestdata['order_amount']:"";
$data2["order_amount"] = $data["order_amount"];

$data["mch_req_time"] = date('Y-m-d H:i:s', time());
$data2["mch_req_time"]=$data["mch_req_time"];

$data["sms_code"] = !empty($requestdata['sms_code'])?$requestdata['sms_code']:"";
$data2["sms_code"] =$data["sms_code"];


$data["order_desc"] = !empty($requestdata['order_desc'])?$requestdata['order_desc']:"";
$data2["order_desc"] =$data["order_desc"] ;

$data["id_type"] = "1";
$data2["id_type"]=$data["id_type"];
//$data["callback_url"] = !empty($requestdata['callback_url'])?$requestdata['callback_url']:"";

$data["callback_url"] = 'http://mpjx.phasercn.com/api/notify';
$data2["callback_url"] =$data["callback_url"] ;

$data["callback_param"] = !empty($requestdata['callback_param'])?$requestdata['callback_param']:"";
$data2["callback_param"] =$data["callback_param"] ;

if(empty($requestdata['payer_name'])){
    $requestdata['payer_name']='';
}
if(empty($requestdata['id_no'])){
    $requestdata['id_no']='';
 }
 if(empty($requestdata['bank_card_no'])){
    $requestdata['bank_card_no']='';
 }
 if(empty($requestdata['mobile_no'])){
    $requestdata['mobile_no']='';
 }


$data["payer_name"] = AESUtil::encryptECB($requestdata['payer_name'], $secKey);//加密
$data2["payer_name"]=$data["payer_name"];

$data["id_no"] = AESUtil::encryptECB($requestdata['id_no'], $secKey);//加密
$data2["id_no"]=$data["id_no"];
$data["bank_card_no"] = AESUtil::encryptECB($requestdata['bank_card_no'], $secKey);//加密
$data2["bank_card_no"]=$data["bank_card_no"];
$data["mobile_no"] = AESUtil::encryptECB($requestdata['mobile_no'], $secKey);//加密
$data2["mobile_no"]=$data["mobile_no"];

$request = new Request();

$ispaid = !empty($requestdata['is_paid'])?$requestdata['is_paid']:"0";

if($ispaid==1){
    //验证过的直接发送支付短信
    //echo '1';
    $request->setMethod("fastPay.agreement.smsPay");
  }else{
     // echo '2';
    //签约流程
    $request->setMethod("fastPay.agreement.smsSign");
  }
$request->setVersion("1.0");
$request->setMchNo("888111200001323");
$request->setSignType("2");
$request->setRandStr(RandomUtil::randomStr(32));
$request->setData($data);
$request->setSecKey($secKey);//rsa有效

$secretKey = new SecretKey();
$secretKey->setReqSignKey($mchPrivateKey);//签名：使用商户私钥
$secretKey->setRespVerifyKey($platPublicKey);//验签：使用平台公钥
$secretKey->setSecKeyEncryptKey($platPublicKey);//sec_key加密：使用平台公钥
$secretKey->setSecKeyDecryptKey($mchPrivateKey);//sec_key解密：使用商户私钥

$url = "https://api.joinpay.com/fastpay";
try {
    $response = RequestUtil::doRequest($url, $request, $secretKey);
    if ($response->isSuccess()) {//受理成功
        

        if ($ispaid==1) {
            $dataArr = json_decode($response->getData(), true);
             
            if($dataArr['order_status']=='P1000'){
                //$msg= '支付成功';
                header('Location: http://mpjx.phasercn.com/admin/Pay/payEnd');

             }else{
                $msg= $dataArr['err_msg'];
            }
           
        } else {
            //订单交易成功
            $request3 = new Request();
            $request3->setMethod("fastPay.agreement.pay");

            $request3->setVersion("1.0");
            $request3->setMchNo("888111200001323");
            $request3->setSignType("2");
            $request3->setRandStr(RandomUtil::randomStr(32));
            $request3->setData($data2);
            $request3->setSecKey($secKey);//rsa有效

            $secretKey3 = new SecretKey();
            $secretKey3->setReqSignKey($mchPrivateKey);//签名：使用商户私钥
            $secretKey3->setRespVerifyKey($platPublicKey);//验签：使用平台公钥
            $secretKey3->setSecKeyEncryptKey($platPublicKey);//sec_key加密：使用平台公钥
            $secretKey3->setSecKeyDecryptKey($mchPrivateKey);//sec_key解密：使用商户私钥

            $response3 = RequestUtil::doRequest($url, $request3, $secretKey3);
            $msg="";
            if ($response3->isSuccess()) {//受理成功

                $dataArr3 = json_decode($response3->getData(), true);
                header('Location: http://mpjx.phasercn.com/admin/Pay/payEnd');
                //print_r( $dataArr3 );
                // if($dataArr3['order_status']=='P1000'){
                //     //$msg= '支付成功';
                //     header('http://mpjx.phasercn.com/admin/Pay/payEnd');
                // }else{
                //     $msg= $dataArr3['err_msg'];
                // }
            //echo '<a href="'.$requestdata['return_url'].'">返回</a>';
            } else {
                 
            //    var_dump($response3);
                $msg=  $response3->getBizMsg();
                // echo $response3->getBizMsg();
            //  echo '<a href="'.$requestdata['return_url'].'">返回</a>';
            }
       
    
        }
    
    }else{
     //   var_dump($response);
       
        $msg=   $response->getBizMsg();
      // echo $response->getBizMsg();
      // echo '<a href="'.$requestdata['return_url'].'">返回</a>';
    }
} catch (Exception $e) {
    print_r($e);
}

?>
<!DOCTYPE HTML>
<title>提示信息</title>
<meta name="charset" content="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="application-name" content="">
<meta name="version" content="ddtouch">
<meta http-equiv="Cache-Control" content="must-revalidate,no-cache">

<style>

    .message_tip{
        text-align: center;
        line-height: 2rem;
        font-size: 1rem;
    }
    .message_tip:first-child{
        margin-top: 10%;
    }

</style>
<html>
<body>
<div class="message_tip"><img src="./message.png"></div>
<div class="message_tip"><?=$msg ?></div>
<div class="message_tip"><a href="http://mpjx.phasercn.com/admin/Pay/payEnd" >返回</a></div>

</body>
</html>